﻿function speak_one(btn) {
    if ($("#audio_player").hasClass("speaking-all")) {

    }
    else {
        if ($(btn).parent().hasClass('speaking')) {
            speak_one_stop(btn)
        }
        else {
            speak_one_play(btn)
        }
    }
}
var speak_one_play = function (btn) {
    var src = $(btn).parent().find(".word-speak-link").html();
    var word_speak_audio = document.getElementById("audio_player");
    $(".word-speak-btn").removeClass("fa-pause").addClass("fa-volume-up");
    $(".word-speak-btn").parent().parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
    $(".word-speak-btn").parent().removeClass("speaking");
    $(btn).removeClass('fa-volume-up').addClass('fa-pause');
    $(btn).parent().parent().addClass("w3-border-top w3-border-bottom w3-border-blue");
    $(btn).parent().addClass("speaking");
    word_speak_audio.src = src;
    word_speak_audio.play();
    word_speak_audio.addEventListener("ended", speak_one_ended, false);
}
var speak_one_stop = function (btn) {
    var word_speak_audio = document.getElementById("audio_player");
    $(btn).removeClass('fa-pause').addClass('fa-volume-up');
    $(btn).parent().parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
    $(btn).parent().removeClass("speaking");
    word_speak_audio.pause();
}
var speak_one_ended = function () {
    if ($("#audio_player").hasClass("speaking-all")) {

    }
    else {
        $("td.speaking .word-speak-btn").removeClass("fa-pause").addClass("fa-volume-up");
        $("td.speaking").parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
        $("td.speaking").removeClass("speaking");
    }
}
var audio = $('#audio_player');
var playlist;
var tracks;
var current;
var firstsong;
function speak_all() {
    current = 0;
    playlist = $('#sortable');
    tracks = playlist.find('tr td.word-speak-item');
    len = tracks.length;
    if (len > 0) {
        if (audio.hasClass("speaking-all")) {
            $(".word-speak-all-btn").removeClass("fa-stop").addClass("fa-play");
            $("td.speaking").find(".word-speak-btn").removeClass("fa-pause").addClass("fa-volume-up");
            $("td.speaking").find(".inline-speak-all-btn").css("display", "none");
            $("td.speaking").parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
            $("td.speaking").removeClass("speaking");
            audio[0].pause();
            audio.removeClass("speaking-all");
        }
        else {
            //speak first word
            if (playlist.find("tr td").hasClass("speaking")) {
                $("td.speaking").find(".word-speak-btn").removeClass("fa-pause").addClass("fa-volume-up");
                $("td.speaking").parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
                $("td.speaking").removeClass("speaking");
            }
            $(".word-speak-all-btn").removeClass("fa-play").addClass("fa-stop");
            audio.addClass("speaking-all");
            firstsong = playlist.find('tr td.word-speak-item')[0];
            speak($(firstsong), audio[0]);
        }
    }
    audio[0].addEventListener('ended', ended_speak_all, false);
}
function ended_speak_all() {
    if (audio.hasClass("speaking-all")) {
        $("td.speaking").find(".word-speak-btn").removeClass("fa-pause").addClass("fa-volume-up");
        $("td.speaking").find(".inline-speak-all-btn").css("display", "none");
        $("td.speaking").parent().removeClass("w3-border-top w3-border-bottom w3-border-blue");
        $("td.speaking").removeClass("speaking");
        current++;
        if (current == len) {
            $(".word-speak-all-btn").removeClass("fa-stop").addClass("fa-play");
            audio.removeClass("speaking-all");
        }
        else {
            link = playlist.find("tr td.word-speak-item")[current];
            speak($(link), audio[0]);
        }
    }
}
function pause_speak_all(btn) {
    if (audio.hasClass("speaking-all")) {
        if ($(btn).parent().hasClass("speaking") && $(btn).hasClass("fa-pause")) {
            audio[0].pause();
            $(btn).removeClass("fa-pause").addClass("fa-volume-up");
        }
        else if ($(btn).parent().hasClass("speaking") && $(btn).hasClass("fa-volume-up")) {
            audio[0].play();
            $(btn).removeClass("fa-volume-up").addClass("fa-pause");
        }
    }
}
function speak(link, player) {
    player.src = link.find(".word-speak-link").html();
    link.find(".word-speak-btn").removeClass("fa-volume-up").addClass("fa-pause");
    link.find(".inline-speak-all-btn").css("display", "inline-block");
    link.parent().addClass("w3-border-top w3-border-bottom w3-border-blue");
    link.addClass("speaking");
    player.load();
    player.play();
}